package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStraregyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatsResult;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStraregyCommendService straregyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        //page
        model.addAttribute("page",strategySearchService.query(qo));
        return "strategy/searchPageTpl";
    }

    @RequestMapping("")
    public String index(Model model){
        //commends 五篇推荐的攻略
        model.addAttribute("commends",straregyCommendService.listTop5());
        //upCds 飙升榜
        model.addAttribute("upCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_UP_SORT));
        //hotCds 热门榜
        model.addAttribute("hotCds",strategyStatsCacheService.listCommends(RedisKey.STRATEGY_STATS_HOT_SORT));

        //abroads 国外
        List<StatsResult> resultList = strategySearchService.listCondition(SearchQueryObject.CONDITION_ABROAD);
        resultList.remove(StatsResult.CHINA);
        model.addAttribute("abroads",resultList);
        //unabroads 国内
        model.addAttribute("unabroads",
                strategySearchService.listCondition(SearchQueryObject.CONDITION_UNABROAD));
        //themes
        model.addAttribute("themes",
                strategySearchService.listCondition(SearchQueryObject.CONDITION_THEME));

        //themeCds
        model.addAttribute("themeCds",strategySearchService.listThemeCommendTop10());

        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Long id,Model model,@LoginUser UserInfo user){
        //每次访问, 飙升值+1
        strategyStatsCacheService.setCdsDatasAsZset(RedisKey.STRATEGY_STATS_UP_SORT,Consts.INCREASE_NUM,id);

        //每次访问这个资源, 阅读数要加1
        strategyStatsCacheService.incrNum(id, Consts.INCREASE_NUM,
                IStrategyStatsCacheService.STRATEGY_STATS_VIEWNUM);

        //从缓存中拿到统计数据, 回显在前端页面上
        model.addAttribute("vo",strategyStatsCacheService.getStratyStats(id));
        //isFavor 显示图标代表, 意思是, 当前用户有没有收藏过这篇文章
        model.addAttribute("isFavor",user != null &&
                strategyStatsCacheService.isFavor(id, user.getId()));

        model.addAttribute("detail",strategyDetailService.getById(id));
        return "strategy/detail";
    }

    //分享加分
    @RequestMapping("/shareAdd")
    @ResponseBody
    public boolean share(Long id) {
        //飙升榜加分
        strategyStatsCacheService.incrNum(id, 1, IStrategyStatsCacheService.STRAGY_STATS_SHARENUM);
        return true;
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long strategyId,Model model,@LoginUser UserInfo user){
        //每次访问, 热门榜数值+1
        strategyStatsCacheService.setCdsDatasAsZset(RedisKey.STRATEGY_STATS_HOT_SORT,Consts.INCREASE_NUM,strategyId);
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        boolean flag = strategyStatsCacheService.strategyThumbup(strategyId,user.getId());
        result.setSuccess(flag);
        //获取顶的总数
        StrategyStats stratyStats = strategyStatsCacheService.getStratyStats(strategyId);
        result.setData(stratyStats);
        return result;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long strategyId,@LoginUser UserInfo user){
        //每次访问, 热门榜数值+1
        strategyStatsCacheService.setCdsDatasAsZset(RedisKey.STRATEGY_STATS_HOT_SORT,Consts.INCREASE_NUM,strategyId);

        AjaxResult result = new AjaxResult();
        //先判断用户是否登录
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        //执行点赞的业务流程, 拿着攻略id和当前用户id
        boolean flag = strategyStatsCacheService.favor(strategyId, user.getId());
        result.setSuccess(flag);
        //获取攻略的统计数据
        StrategyStats stats = strategyStatsCacheService.getStratyStats(strategyId);
        result.setData(stats);
        //result 里面的success 不是代表操作成败, 代表的是 判断此次操作是收藏操作还是取消操作
        return result;
    }

    @RequestMapping("list")
    public String index(@ModelAttribute("qo") StrategyQueryObject qo, Model model){
        //tags, 根据目的地id查询它所有的标签
        List<StrategyTag> tags = strategyTagService.selectByDestId(qo.getDestId());
        model.addAttribute("tags",tags);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        // 吐司toasts
        model.addAttribute("toasts",toasts);
        // 目的地dest
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @RequiredLogin
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @LoginUser UserInfo user){
        //每次访问, 飙升值+1
        strategyStatsCacheService.setCdsDatasAsZset(RedisKey.STRATEGY_STATS_UP_SORT,
                Consts.INCREASE_NUM,comment.getDetailId());

        AjaxResult result = new AjaxResult();
        //设置用户数据
        comment.setUserId(user.getId());
        comment.setUsername(user.getNickname());
        comment.setLevel(user.getLevel());
        comment.setHeadUrl(user.getHeadImgUrl());

        //新增一条评论
        strategyCommentService.save(comment);

        //每次新增一条评论的时候, 缓存也要更新,评论数加1, 返回增加之后的vo对象
        Long detailId = comment.getDetailId();
        strategyStatsCacheService.incrNum(detailId,Consts.INCREASE_NUM,
                IStrategyStatsCacheService.STRATEGY_STATS_REPLYNUM);

        //获取攻略的统计数据
        StrategyStats stats = strategyStatsCacheService.getStratyStats(detailId);

        //addData()方法才返回result对象,是setData()返回值是void
        return result.addData(stats.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(@ModelAttribute("qo") StrategyCommentQueryObject qo, Model model){
        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    @RequestMapping("commentThumbUp")
    @RequiredLogin
    @ResponseBody
    public Object commentThumbUp(String toId, Long fromId){
        strategyCommentService.commentThumbUp(toId,fromId);
        return new AjaxResult();
    }
}
